#include "napi/native_api.h"
#include <stdint.h>
// #include "modbus/test_main.h"
extern "C" {
#include "modbus/test_main.h"
}
static napi_value init_connect(napi_env env, napi_callback_info info) {
    size_t argc = 2;
    napi_value args[2] = {nullptr};

    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);

    napi_valuetype valuetype0;
    napi_typeof(env, args[0], &valuetype0);

    napi_valuetype valuetype1;
    napi_typeof(env, args[1], &valuetype1);

    size_t typeLen;
    napi_get_value_string_utf8(env, args[0], nullptr, 0, &typeLen);
    // 创建存储字符串的数组，数组大小为“字符串长度+1”, 1代表字符串结束符
    char *extContent = new char[typeLen + 1];

    // 解析TS的字符串参数，将内容赋值到char数组中
    napi_get_value_string_utf8(env, args[0], extContent, typeLen + 1, &typeLen);
    napi_value value0;
    // 将字符串赋值给napi_value
    napi_create_string_utf8(env, extContent, typeLen + 1, &value0);

    int value1;
    napi_get_value_int32(env, args[1], &value1);

    int d = modbusInitConnect(extContent, value1);
    napi_value sum;
    napi_create_double(env, d, &sum);
    // 释放数组空间
    delete[] extContent;
    return sum;
}

static napi_value close_connect(napi_env env, napi_callback_info info) {
        size_t argc = 2;
        napi_value args[2] = {nullptr};
    
        napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    int d = closeConnect();
    napi_value sum;
    napi_create_double(env, d, &sum);
    return sum;
}
static napi_value receive_data(napi_env env, napi_callback_info info) {
        size_t argc = 2;
        napi_value args[2] = {nullptr};
    
        napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);

    napi_valuetype valuetype0;
    napi_typeof(env, args[0], &valuetype0);

    napi_valuetype valuetype1;
    napi_typeof(env, args[1], &valuetype1);

    double value0;
    napi_get_value_double(env, args[0], &value0);

    double value1;
    napi_get_value_double(env, args[1], &value1);

    uint16_t *values = receiveData();
   size_t array_length = sizeof(values) ;
   
       // 创建一个 NAPI 数组
       napi_value result;
       napi_create_array_with_length(env, array_length, &result);
   
       // 将 uint16_t 数组的元素逐个添加到 NAPI 数组中
       for (size_t i = 0; i < array_length; i++) {
           napi_value element;
           napi_create_uint32(env, values[i], &element);
           napi_set_element(env, result, i, element);
       }

    return result;
}
static napi_value send_data(napi_env env, napi_callback_info info) {
        size_t argc = 2;
        napi_value args[2] = {nullptr};
    
        napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);

    napi_valuetype valuetype0;
    napi_typeof(env, args[0], &valuetype0);

    napi_valuetype valuetype1;
    napi_typeof(env, args[1], &valuetype1);

    double value0;
    napi_get_value_double(env, args[0], &value0);

    double value1;
    napi_get_value_double(env, args[1], &value1);

   int res = sendData(value0,value1);
    napi_value sum;
    napi_create_int32(env, res, &sum);
    // 释放数组空间
    return sum;
}

EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports) {
    napi_property_descriptor desc[] = {
        {"init_connect", nullptr, init_connect, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"close_connect", nullptr, close_connect, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"receive_data", nullptr, receive_data, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"send_data", nullptr, send_data, nullptr, nullptr, nullptr, napi_default, nullptr}
    };

    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
    return exports;
}
EXTERN_C_END

static napi_module demoModule = {
    .nm_version = 1,
    .nm_flags = 0,
    .nm_filename = nullptr,
    .nm_register_func = Init,
    .nm_modname = "entry",
    .nm_priv = ((void *)0),
    .reserved = {0},
};

extern "C" __attribute__((constructor)) void RegisterEntryModule(void) {
    napi_module_register(&demoModule);
}
